package com.hornsun.client.personalPage.service;

import com.google.common.base.Preconditions;
import com.hornsun.domain.dbo.HealthBeansRecord;
import com.hornsun.repository.HealthBeansReposiory;
import com.hornsun.repository.MemberWechatRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by 编译中... on 2018/3/22.
 */
@Service
public class HealthBeansService {
    @Autowired
    private MemberWechatRepository memberWechatRepository;

    @Autowired
    private HealthBeansReposiory healthBeansReposiory;

    /**
     * 查询健康豆的数目
     * @param openid
     * @return
     */
    public Integer queryHealthBeans(String openid){
        Preconditions.checkNotNull(openid);
        return memberWechatRepository.selectByOpenid(openid).getHealthBeans();
    }

    /**
     *查询指定用户的健康豆收支记录
     * @param openid
     * @return
     */
    public List<HealthBeansRecord> queryHealthBeansRecord(String openid,int page){
        Preconditions.checkNotNull(openid);
        Preconditions.checkNotNull(page);
        return healthBeansReposiory.getHealthBeansRecord(openid,page);
    }


    /**
     * 收获健康豆
     * @param openid
     * @param count
     * @param source
     */
    public void gainHealthBeans(String openid,int count,String source){
        HealthBeansRecord healthBeansRecord = new HealthBeansRecord();
        healthBeansRecord.setOpenid(openid);
        healthBeansRecord.setCount(count);
        healthBeansRecord.setSource(source);
        healthBeansRecord.setRecordTime(new Date());
        healthBeansReposiory.addHealthBeans(openid,count);
        healthBeansReposiory.insertHealthBeansRecord(healthBeansRecord);
    }


}